package com.ruoyi.procurementmanagement.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.procurementmanagement.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procurementmanagement.service.IEsProcurementPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购计划(es_procurement_plan)Controller
 *
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/procurementmanagement/plan")
public class EsProcurementPlanController extends BaseController
{
    @Autowired
    private IEsProcurementPlanService esProcurementPlanService;

    /**
     * 查询采购计划(es_procurement_plan)列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(EsProcurementPlan esProcurementPlan)
    {
        startPage();
        List<EsProcurementPlan> list = esProcurementPlanService.selectEsProcurementPlanList(esProcurementPlan);
        return getDataTable(list);
    }

    /**
     * 导出采购计划(es_procurement_plan)列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:export')")
    @Log(title = "采购计划(es_procurement_plan)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsProcurementPlan esProcurementPlan)
    {
        List<EsProcurementPlan> list = esProcurementPlanService.selectEsProcurementPlanList(esProcurementPlan);
        ExcelUtil<EsProcurementPlan> util = new ExcelUtil<EsProcurementPlan>(EsProcurementPlan.class);
        util.exportExcel(response, list, "采购计划(es_procurement_plan)数据");
    }

    /**
     * 获取采购计划(es_procurement_plan)详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        EsProcurementPlan esProcurementPlan = esProcurementPlanService.selectEsProcurementPlanById(id);
        // 查询关联的采购明细
        List<EsProcurementPlanProduct> productList = esProcurementPlanService.selectPlanProductByPlanId(id);
        esProcurementPlan.setProductList(productList);
        return success(esProcurementPlan);
    }

    /**
     * 新增采购计划(es_procurement_plan)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:add')")
    @Log(title = "采购计划(es_procurement_plan)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsProcurementPlan esProcurementPlan) {
        return toAjax(esProcurementPlanService.insertEsProcurementPlan(esProcurementPlan));
    }

    /**
     * 修改采购计划(es_procurement_plan)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:edit')")
    @Log(title = "采购计划(es_procurement_plan)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsProcurementPlan esProcurementPlan) {
        return toAjax(esProcurementPlanService.updateEsProcurementPlan(esProcurementPlan));
    }

    /**
     * 删除采购计划(es_procurement_plan)
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:remove')")
    @Log(title = "采购计划(es_procurement_plan)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(esProcurementPlanService.deleteEsProcurementPlanByIds(ids));
    }

    /**
     * 获取部门层级数据
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:tree')")
    @GetMapping("/tree")
    public AjaxResult getDeptTree() {
        List<SystemsDept> depts = esProcurementPlanService.selectDeptTree();
        return AjaxResult.success(depts);
    }

    /**
     * 获取编制人列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:compiler')")
    @GetMapping("/compiler")
    public AjaxResult getPersonList()
    {
        System.out.println("获取编制人列表");
        List<EsAdminUser> persons = esProcurementPlanService.selectEsAdminUserList();
        return AjaxResult.success(persons);
    }

    /**
     * 获取供应商列表
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:supplier')")
    @GetMapping("/supplier")
    public AjaxResult getsupplierList()
    {
        List<ErpSuppliers> supplier = esProcurementPlanService.selectErpSuppliersList();
        return AjaxResult.success(supplier);
    }

    /**
     * 获取供应商列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurementmanagement:plan:spquery')")
    @GetMapping(value = "/{id}/supplierListBid")  // 添加子路径
    public AjaxResult getsupplierListBid(@PathVariable("id") Integer id) {
        ErpSuppliers supplierDetail = esProcurementPlanService.selectErpSuppliersListById(id);
        if (supplierDetail == null) {
            return AjaxResult.error("未找到供应商详情");
        }
        return AjaxResult.success(supplierDetail);
    }
}
